<?php



namespace app\index\controller;

use app\api\service\payment\Gpay;
use app\common\library\RsaUtils;
use function GuzzleHttp\Promise\all;
use function GuzzleHttp\json_decode;
use Zxing\Result;

class Test extends Base
{
    public function _initialize()
    {
    }

    public function index()
    {
        return $this->fetch();
    }

    /**
     * 测试接口
     *
     *
     * @return mixed
     */
    public function api($code)
    {
        if ($this->request->isPost()) {
            $apiInfo = $this->logicApi->getApiInfo(["uid" => $this->request->post("uid")]);
            if (!empty($apiInfo)) {
                $url = $this->request->header()["origin"] . '/pay/order';
                $appid = $apiInfo["uid"];
                $key = $apiInfo["key"];
                $amount = $this->request->post("amount");

                $result = $this->test_api_pay($url, $appid, $key, $code, $amount);
                $result->type = $code;

                $this->result(1,  '提交申请付款成功。', $result);
            } else {
                $this->result(0,  '商户信息不能存在，请确认商户编号正确。');
            }
        }

        $codeInfo =  $this->logicPay->getCode(["name" => $code]);
        $this->assign('code', $codeInfo);
        return $this->fetch();
    }

    private function test_api_pay($url, $appid, $key, $pay_type, $order_amt)
    {
        $order_id = create_order_no(); //订单号
        $notify_url = 'notify_url'; //异步通知地址
        $return_url = "{$_SERVER['REQUEST_SCHEME']}://{$_SERVER['HTTP_HOST']}/success"; //同步通知地址
        $goods_name = '测试商品'; //物品名称
        $goods_note = '测试商品描述'; //物品名称
        $extends = ''; //物品名称
        $time_stamp = date("YmdHis", time()); //时间戳

        $sign = 'app_id=' . $appid
            . '&pay_type=' . $pay_type
            . '&order_id=' . $order_id
            . '&order_amt=' . $order_amt
            . '&notify_url=' . $notify_url
            . '&return_url=' . $return_url
            . '&time_stamp=' . $time_stamp
            . '&key=' . $key;

        $param = array(
            'app_id' => $appid,
            'pay_type' => $pay_type,
            'order_id' => $order_id,
            'order_amt' => $order_amt,
            'notify_url' => urlencode($notify_url),
            'return_url' => urlencode($return_url),
            'goods_name' => urlencode($goods_name),
            'goods_note' => urlencode($goods_note),
            'extends' => urlencode($extends),
            'user_ip' => "127.0.0.1",
            'time_stamp' => $time_stamp,
            'sign' => md5($sign)
        );

        $postdata = http_build_query($param);

        $options = array(
            'http' => array(
                'method' => 'POST',
                'header' => 'Content-type:application/x-www-form-urlencoded',
                'content' => $postdata,
                'timeout' => 15 * 60 // 超时时间（单位:s）    
            )
        );

        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);

        return json_decode($result);
    }
}
